
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>1.7 替换与正则编译 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="正则表达式练习.html" />
    
    
    <link rel="prev" href="标记.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="正则表达式.html">
            
                <a href="正则表达式.html">
            
                    
                    第一章：正则表达式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="正则表达式入门.html">
            
                <a href="正则表达式入门.html">
            
                    
                    1.1 正则表达式入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="Python中的正则表达式.html">
            
                <a href="Python中的正则表达式.html">
            
                    
                    1.2 Python中的正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="基本正则表达式.html">
            
                <a href="基本正则表达式.html">
            
                    
                    1.3 基本正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="分组.html">
            
                <a href="分组.html">
            
                    
                    1.4 分组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="先行断言.html">
            
                <a href="先行断言.html">
            
                    
                    1.5 先行断言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="标记.html">
            
                <a href="标记.html">
            
                    
                    1.6 标记
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.7" data-path="替换与正则表达式编译.html">
            
                <a href="替换与正则表达式编译.html">
            
                    
                    1.7 替换与正则编译
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="正则表达式练习.html">
            
                <a href="正则表达式练习.html">
            
                    
                    1.8 正则表达式练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Python爬虫学习目标.html">
            
                <a href="Python爬虫学习目标.html">
            
                    
                    第二章：爬虫与Requests
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Requests的简单使用.html">
            
                <a href="Requests的简单使用.html">
            
                    
                    2.1 Requests的简单使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="深入了解Requests.html">
            
                <a href="深入了解Requests.html">
            
                    
                    2.2 深入了解Requets
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="结构化与非结构化.html">
            
                <a href="结构化与非结构化.html">
            
                    
                    第三章：非结构化与结构化数据提取
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="正则抓取.html">
            
                <a href="正则抓取.html">
            
                    
                    3.1 使用正则的爬虫
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="xpath与XML.html">
            
                <a href="xpath与XML.html">
            
                    
                    3.2 Xpath与XML类库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="lxml.html">
            
                <a href="lxml.html">
            
                    
                    3.3 LXML库的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Xpath爬虫案例.html">
            
                <a href="Xpath爬虫案例.html">
            
                    
                    3.4 案例：贴吧图片
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="Json与JsonPath.html">
            
                <a href="Json与JsonPath.html">
            
                    
                    3.5 Json与Json数据获取
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="实例：糗事百科网站抓取.html">
            
                <a href="实例：糗事百科网站抓取.html">
            
                    
                    3.6 糗事百科网站爬虫
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >1.7 替换与正则编译</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x66FF;&#x6362;">&#x66FF;&#x6362;</h2>
<p>&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x5F15;&#x64CE;&#x5E76;&#x4E0D;&#x4EC5;&#x4EC5;&#x5C40;&#x9650;&#x4E8E;&#x8BC6;&#x522B;&#x4E00;&#x4E2A;&#x6A21;&#x5F0F;&#x662F;&#x5426;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x5B58;&#x5728;&#xFF0C;&#x5B83;&#x8FD8;&#x80FD;&#x591F;&#x6267;&#x884C;&#x5B57;&#x7B26;&#x4E32;&#x66FF;&#x6362;&#xFF0C;&#x57FA;&#x4E8E;&#x5728;&#x539F;&#x59CB;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x5206;&#x7EC4;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<p>Python&#x4E2D;&#x7684;&#x66FF;&#x6362;&#x65B9;&#x6CD5;&#x662F;<code>re.sub</code>&#xFF0C;&#x4ED6;&#x63A5;&#x6536;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x7528;&#x4E8E;&#x66FF;&#x6362;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x88AB;&#x641C;&#x7D22;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<p>&#x53EA;&#x8981;&#x5B9E;&#x9645;&#x5339;&#x914D;&#x88AB;&#x66FF;&#x6362;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5339;&#x914D;&#xFF0C;&#x5219;<code>re.sub</code>&#x6700;&#x7EC8;&#x4E0D;&#x6267;&#x884C;&#x4EFB;&#x4F55;&#x64CD;&#x4F5C;&#x3002;</p>
<p><code>re.sub</code>&#x5141;&#x8BB8;&#x4ECE;&#x88AB;&#x66FF;&#x6362;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x6A21;&#x5F0F;&#x4F7F;&#x7528;&#x540C;&#x6837;&#x7684;&#x56DE;&#x6EAF;&#x5F15;&#x7528;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x6211;&#x4EEC;&#x770B;&#x4E00;&#x4E2A;&#x4ECE;&#x7535;&#x8BDD;&#x53F7;&#x7801;&#x4E2D;&#x5265;&#x79BB;&#x65E0;&#x5173;&#x683C;&#x5F0F;&#x6570;&#x636E;&#x7684;&#x4EFB;&#x52A1;&#x3002;</p>
<pre><code class="lang-python">a = re.sub(<span class="hljs-string">r&apos;[\d]+&apos;</span>,<span class="hljs-string">r&apos;a&apos;</span>,<span class="hljs-string">&apos;213-667-8890&apos;</span>)

b = re.sub(<span class="hljs-string">r&apos;(\+?1?)[ .-]?\(?([\d]{3})\)?[ .-]?([\d]{3})[ .-]?([\d]{4})&apos;</span>,<span class="hljs-string">r&apos;\2\3\4&apos;</span>,<span class="hljs-string">&apos;213-667-8890&apos;</span>)

c = re.sub(<span class="hljs-string">r&apos;(\+?1?)[ .-]?\(?([\d]{3})\)?[ .-]?([\d]{3})[ .-]?([\d]{4})&apos;</span>,<span class="hljs-string">r&apos;\2\3\4&apos;</span>,<span class="hljs-string">&apos;(213)8675509&apos;</span>)

d = re.sub(<span class="hljs-string">r&apos;(\+?1?)[ .-]?\(?([\d]{3})\)?[ .-]?([\d]{3})[ .-]?([\d]{4})&apos;</span>,<span class="hljs-string">r&apos;\2\3\4&apos;</span>,<span class="hljs-string">&apos;213.867.5509&apos;</span>)

e = re.sub(<span class="hljs-string">r&apos;(\+?1?)[ .-]?\(?([\d]{3})\)?[ .-]?([\d]{3})[ .-]?([\d]{4})&apos;</span>,<span class="hljs-string">r&apos;\2\3\4&apos;</span>,<span class="hljs-string">&apos;+1-213-854-5557&apos;</span>)

f = re.sub(<span class="hljs-string">r&apos;(\+?1?)[ .-]?\(?([\d]{3})\)?[ .-]?([\d]{3})[ .-]?([\d]{4})&apos;</span>,<span class="hljs-string">r&apos;\2\3\4&apos;</span>,<span class="hljs-string">&apos;1213-854-5557&apos;</span>)


print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
</code></pre>
<pre><code>a-a-a
2136678890
2138675509
2138675509
2138545557
2138545557
</code></pre><pre><code class="lang-python"><span class="hljs-comment"># &#x8FD8;&#x53EF;&#x4EE5;&#x5229;&#x7528;sub&#x7684;&#x529F;&#x80FD;&#x4F7F;&#x6240;&#x6709;&#x7684;&#x7535;&#x8BDD;&#x53F7;&#x7801;&#x683C;&#x5F0F;&#x4E00;&#x81F4;</span>
b = re.sub(<span class="hljs-string">r&apos;(\+?1?)[ .-]?\(?([\d]{3})\)?[ .-]?([\d]{3})[ .-]?([\d]{4})&apos;</span>,<span class="hljs-string">r&apos;(\2)\3-\4&apos;</span>,<span class="hljs-string">&apos;213-667-8890&apos;</span>)

c = re.sub(<span class="hljs-string">r&apos;(\+?1?)[ .-]?\(?([\d]{3})\)?[ .-]?([\d]{3})[ .-]?([\d]{4})&apos;</span>,<span class="hljs-string">r&apos;(\2)\3-\4&apos;</span>,<span class="hljs-string">&apos;(213)8675509&apos;</span>)

d = re.sub(<span class="hljs-string">r&apos;(\+?1?)[ .-]?\(?([\d]{3})\)?[ .-]?([\d]{3})[ .-]?([\d]{4})&apos;</span>,<span class="hljs-string">r&apos;(\2)\3-\4&apos;</span>,<span class="hljs-string">&apos;213.867.5509&apos;</span>)

e = re.sub(<span class="hljs-string">r&apos;(\+?1?)[ .-]?\(?([\d]{3})\)?[ .-]?([\d]{3})[ .-]?([\d]{4})&apos;</span>,<span class="hljs-string">r&apos;(\2)\3-\4&apos;</span>,<span class="hljs-string">&apos;+1-213-854-5557&apos;</span>)

f = re.sub(<span class="hljs-string">r&apos;(\+?1?)[ .-]?\(?([\d]{3})\)?[ .-]?([\d]{3})[ .-]?([\d]{4})&apos;</span>,<span class="hljs-string">r&apos;(\2)\3-\4&apos;</span>,<span class="hljs-string">&apos;1213-854-5557&apos;</span>)

print(b)
print(c)
print(d)
print(e)
print(f)
</code></pre>
<pre><code>(213)667-8890
(213)867-5509
(213)867-5509
(213)854-5557
(213)854-5557
</code></pre><p>&#x53EF;&#x4EE5;&#x53D1;&#x73B0;&#x88AB;&#x66FF;&#x6362;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x4E0D;&#x4EC5;&#x9650;&#x4E8E;&#x4F7F;&#x7528;&#x4E8E;&#x5BF9;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x56DE;&#x6EAF;&#x5F15;&#x7528;&#xFF0C;&#x5176;&#x4ED6;&#x5B57;&#x7B26;&#x6309;&#x5B57;&#x9762;&#x88AB;&#x89E3;&#x91CA;&#xFF0C;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E5F;&#x53EF;&#x4EE5;&#x770B;&#x51FA;re.sub&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x683C;&#x5F0F;&#x5316;&#x6570;&#x636E;&#xFF0C;&#x662F;&#x591A;&#x79CD;&#x4E0D;&#x540C;&#x7684;&#x6570;&#x636E;&#x663E;&#x793A;</p>
<h2 id="&#x5DF2;&#x7F16;&#x8BD1;&#x7684;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;">&#x5DF2;&#x7F16;&#x8BD1;&#x7684;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;</h2>
<p><code>re</code>&#x6A21;&#x5757;&#x5305;&#x542B;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF1A;<code>compile</code>&#xFF0C;&#x5B83;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5DF2;&#x7F16;&#x8BD1;&#x7684;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x5BF9;&#x8C61;&#xFF0C;&#x8BE5;&#x5BF9;&#x8C61;&#x4E4B;&#x540E;&#x53EF;&#x4EE5;&#x88AB;&#x590D;&#x7528;&#x3002;</p>
<pre><code class="lang-python">regex = re.compile(<span class="hljs-string">r&apos;[\d]{3,4}&apos;</span>)

a = regex.search(<span class="hljs-string">&apos;578&apos;</span>)
b = regex.search(<span class="hljs-string">&apos;&#x7F16;&#x53F7;8975&apos;</span>)

print(a)
print(b)
</code></pre>
<pre><code>&lt;re.Match object; span=(0, 3), match=&apos;578&apos;&gt;
&lt;re.Match object; span=(2, 6), match=&apos;8975&apos;&gt;
</code></pre><p>&#x4F7F;&#x7528;&#x5DF2;&#x7F16;&#x8BD1;&#x7684;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x6709;&#x4E24;&#x4E2A;&#x597D;&#x5904;&#xFF1A;</p>
<ol>
<li><p>&#x53EF;&#x7528;&#x4E8E;&#x4F5C;&#x4E3A;&#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x88AB;&#x4F20;&#x9012;</p>
</li>
<li><p>&#x5141;&#x8BB8;&#x4F7F;&#x7528;&#x5728;re.search&#x5141;&#x8BB8;&#x4F7F;&#x7528;&#x5728;re.search&#x4E2D;&#x4E0D;&#x53EF;&#x7528;&#x7684;&#x4E24;&#x4E2A;&#x989D;&#x5916;&#x53C2;&#x6570;&#xFF0C;&#x8FD9;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#x5206;&#x522B;&#x662F;&#x88AB;&#x641C;&#x7D22;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5F00;&#x59CB;&#x548C;&#x7ED3;&#x675F;&#x4F4D;&#x7F6E;&#xFF0C;&#x4ED6;&#x4EEC;&#x53EF;&#x7528;&#x6765;&#x51CF;&#x5C11;&#x5BF9;&#x90E8;&#x5206;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x8003;&#x8651;&#x3002;</p>
</li>
</ol>
<pre><code class="lang-python">regex = re.compile(<span class="hljs-string">&apos;[\d]+&apos;</span>)
a = regex.search(<span class="hljs-string">&apos;1 &#x7F16;&#x53F7;89757&#xFF0C;&#x4ECE;&#x8FD9;&#x4E00;&#x523B;&#x5C31;&#x662F;&#x4F60;&#x7ED9;&#x6211;&#x7684;&#x59D3;&#x540D;&apos;</span>)
b = regex.search(<span class="hljs-string">&apos;1 &#x7F16;&#x53F7;89757&#xFF0C;&#x4ECE;&#x8FD9;&#x4E00;&#x523B;&#x5C31;&#x662F;&#x4F60;&#x7ED9;&#x6211;&#x7684;&#x59D3;&#x540D;&apos;</span>,pos=<span class="hljs-number">2</span>)

<span class="hljs-comment">#pos&#xFF1A;&#x5339;&#x914D;&#x7684;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;&#xFF0C;&#x53EF;&#x9009;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A;0</span>
<span class="hljs-comment">#endpos&#xFF1A;&#x5339;&#x914D;&#x7684;&#x7ED3;&#x675F;&#x4F4D;&#x7F6E;&#xFF0C;&#x53EF;&#x9009;&#xFF0C;&#x9ED8;&#x8BA4;&#x4E3A; len(string)</span>
print(a)
print(b)
</code></pre>
<pre><code>&lt;re.Match object; span=(0, 1), match=&apos;1&apos;&gt;
&lt;re.Match object; span=(4, 9), match=&apos;89757&apos;&gt;
</code></pre>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="标记.html" class="navigation navigation-prev " aria-label="Previous page: 1.6 标记">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="正则表达式练习.html" class="navigation navigation-next " aria-label="Next page: 1.8 正则表达式练习">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"1.7 替换与正则编译","level":"1.2.7","depth":2,"next":{"title":"1.8 正则表达式练习","level":"1.2.8","depth":2,"path":"notes/正则表达式练习.md","ref":"notes/正则表达式练习.md","articles":[]},"previous":{"title":"1.6 标记","level":"1.2.6","depth":2,"path":"notes/标记.md","ref":"notes/标记.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"notes/替换与正则表达式编译.md","mtime":"2019-04-28T15:04:08.006Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-05-08T02:50:45.307Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

